package view;

import brain.SortManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import simulation.Mover;
import simulation.Painter;


/**
 * @author  Owner
 */
public class Runner {

    private static Runner inst;

	private static View view;
    
    private FormData fd;
    
    private Label stop;
    
    private Label skip;
    
    private boolean simMode;
    private boolean pause;
    
    private Label playPause;

    private Scale scale;
    
	private Runner() {
	}
	
	public static Runner getInstance(){
	    if(inst==null)
		inst=new Runner();
	    return inst;
	}
	
	public static void main(String[] args) {
	    view =View.getInstance();
	    view.initView();
	}
	
	public void createAnimationBar(Composite parent){
	    
	    Color color = Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND);
	    parent.setBackground(color);
	    Group animGp = new Group(parent,  SWT.SHADOW_ETCHED_OUT | SWT.SHADOW_IN |SWT.SHADOW_OUT);
	    animGp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	    animGp.setBackground(color);
	    
	    animGp.setLayout(new FormLayout());
	    
	    fd = new FormData();
	    fd.left = new FormAttachment(50);
	    fd.width = 1;
	    fd.height = 20;
	    Composite center = new Composite(animGp, SWT.NONE);
	    center.setLayoutData(fd);
	    
	    //stop button:
	    fd = new FormData();
	    fd.right = new FormAttachment(center, -70);
	    fd.width = 32;
	    fd.height = 32;
	    this.stop = new Label(animGp, SWT.NONE);
	    stop.setLayoutData(fd);
	    this.stop.setBackground(color);
	    stop.setImage(new Image(null, getClass().getResourceAsStream("stop.png")));
	    stop.setToolTipText("Stop");
	    
	    //skip button:
	    fd = new FormData();
	    fd.left = new FormAttachment(90);
	    fd.width = 32;
	    fd.height = 32;
	    this.skip = new Label(animGp, SWT.NONE);
	    skip.setLayoutData(fd);
	    this.skip.setBackground(color);
	    skip.setImage(new Image(null, getClass().getResourceAsStream("skip.png")));
	    skip.setToolTipText("       Skip Current\nSimulation Technique");
	    
	    //play-pause button:
	    fd = new FormData();
	    fd.left = new FormAttachment(center, 70);
	    fd.width = 32;
	    fd.height = 32;
	    this.playPause = new Label(animGp, SWT.NONE);
	    playPause.setLayoutData(fd);
	    this.playPause.setBackground(color);
	    playPause.setImage(new Image(null, getClass().getResourceAsStream("play.png")));
	    playPause.setToolTipText("Play/Pause");
	    
	    //speed controller:
	    fd = new FormData();
	    fd.width = 140;
	    fd.right = new FormAttachment(center, 70);
	     scale = new Scale (animGp, SWT.NONE);
	    scale.setBackground(color);
	    scale.setMaximum (20);
	    scale.setPageIncrement (1);
	    scale.setLayoutData(fd);
	    scale.setSelection(10);
	    
	    addListeners();
	    
	}
	
	public void setPausePlayImg(String imgPath){
	    playPause.setImage(new Image(null, getClass().getResourceAsStream(imgPath)));
	}
	
	private void addListeners(){
	
	//pause play
    	this.playPause.addMouseListener(new MouseListener(){
    	    public void mouseDoubleClick(MouseEvent e) {
    	    }
    	    public void mouseDown(MouseEvent e) {
    		if(!pause && simMode){
    		    SortManager.getInstance().pauseSimulation();
    		    pause = true;
    		    setPausePlayImg("play.png");
    		    Painter.getInstance().setPaused(true);
//    		    View.getInstance().getDisplay().asyncExec(new Runnable(){
    		    	 View.getInstance().getCanvas().redraw();
//    		    });
    		}
    		else if(simMode){
    		    SortManager.getInstance().resumeSimulation();
    		    pause = false;
    		    setPausePlayImg("pause.png");
    		    Painter.getInstance().setPaused(false);
    		    View.getInstance().getCanvas().redraw();
    		}
    	    }
    	    public void mouseUp(MouseEvent e) {
    	    }
    	});
    	
    	
    	//skip button:
    	this.skip.addMouseListener(new MouseListener(){
	    public void mouseDoubleClick(MouseEvent arg0) {
	    }
	    public void mouseDown(MouseEvent arg0 ) {
		if(simMode)
		    SortManager.getInstance().skipSimulation();
			
	    }
	    public void mouseUp(MouseEvent arg0) {
	    }});
    	
    	//stop button:
    	this.stop.addMouseListener(new MouseListener(){
	    public void mouseDoubleClick(MouseEvent arg0) {
	    }
	    public void mouseDown(MouseEvent arg0) {
		if(simMode)
		    SortManager.getInstance().stopSimulation();
	    }
	    public void mouseUp(MouseEvent arg0) {
	    }});
    	
    	//slider:
    	this.scale.addSelectionListener(new SelectionListener(){
	    public void widgetDefaultSelected(SelectionEvent arg0) {
	    }
	    public void widgetSelected(SelectionEvent arg0) {
		Mover.setSpeed(20 - scale.getSelection());
	    }});
    	
	}

	/**
	 * @param simMode  the simMode to set
	 * @uml.property  name="simMode"
	 */
	public void setSimMode(boolean b) {
	    this.simMode=b;
	}

	/**
	 * @return  the simMode
	 * @uml.property  name="simMode"
	 */
	public boolean getSimMode(){
		return simMode; 
	}
	
}
